<?php

/**
 * Settings - Change Photo
 * 
 * @project : Loobica
 * @author  : Dino
 * @date    : 27 Mar 2014
 * 
 */
if (!defined('BASEPATH'))
  exit('No direct script access allowed');

class Photo extends LB_Auth_Controller {

  /**
   * Current User Photo Change
   * 
   */
  public function index() {

    $status = false;

    // Form Submit
    if ($this->is_post()) {

      // Getting New Image
      $img_array = (array) $this->input->post('user_image_id');

      // Checking Image
      if (!empty($img_array)) {

        // New File
        $new_image_file = (string) end($img_array);
        $new_image_path = (string) USER_IMG_FOLDER . $new_image_file;

        // Check New File
        if (file_exists($new_image_path)) {

          // Deleting Old Image
          unlink(USER_IMG_FOLDER . $this->signed_user['user_image']);

          // Setting New Image
          $user_update = array();
          $user_update['user_image'] = $new_image_file;
          $this->user_model->set_user($user_update, $this->signed_user['user_id']);

          // Set success status
          $status = true;
        }
      }
    }

    // Getting user data after update
    $user_data = $this->user_model->get_user_id($this->signed_user['user_id'], 1, true);

    // Preparing view data
    $view_data = array();
    $view_data['title'] = 'Settings - Photo';
    $view_data['status'] = $status;
    $view_data['user_data'] = $user_data;

    // Loading view
    $this->load_view('settings/photo', $view_data);
    return;
  }

  /**
   * Upload API
   * 
   */
  public function upload() {

    // sleep(3);
    // Upload Config
    $config = array();
    $config['upload_path'] = TEMP_IMG_FOLDER;
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '5120';
    $this->load->library('upload', $config);

    // Upload
    if ($this->upload->do_upload('user_image_file')) {

      $data = array('upload_data' => $this->upload->data());

      if (!empty($data)) {

        $img_key = $this->loobica_hash(microtime());
        $img_name = $img_key . '.jpg';
        $img_file = USER_IMG_FOLDER . $img_name;
        $temp_file = $data['upload_data']['full_path'];

        // Generate Cropped Clean JPEG Image
        $this->load->library('loobica/user_image');
        $user_image = $this->user_image->size(250, 250)->generate($temp_file, $img_file, null, true);

        // Updating user
        if ($user_image) {

          // Delete temp file
          unlink($temp_file);

          // Success - Respond file name
          die(json_encode(array('file' => $img_name)));
        }
      }
    }

    // Error - Respond error
    die(json_encode(array('error' => 'Upload failed. Try again later !')));
  }

  /**
   * Delete User Photo
   * 
   */
  public function delete() {

    // Getting Old Image
    $old_file = USER_IMG_FOLDER . $this->signed_user['user_image'];

    // Deleting file
    if (file_exists($old_file)) {
      unlink($old_file);
    }

    // Clearing Image
    $user_update = array();
    $user_update['user_image'] = null;
    $this->user_model->set_user($user_update, $this->signed_user['user_id']);

    // Redirect Back
    $this->redirect('settings/photo');
  }

}

?>